package com.ruoyi.system.service.impl;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TeacherMapper;
import com.ruoyi.system.domain.Teacher;
import com.ruoyi.system.service.ITeacherService;
import com.ruoyi.common.core.text.Convert;

import javax.validation.Validator;

/**
 * 老师Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-03
 */
@Service
public class TeacherServiceImpl implements ITeacherService 
{
    private static final Logger log = LoggerFactory.getLogger(TeacherServiceImpl.class);

    @Autowired
    private TeacherMapper teacherMapper;

    private String getCacheName()
    {
        return Constants.SYS_CONFIG_CACHE;
    }

    private String getCacheKey(String teacherKey)
    {
        return Constants.SYS_CONFIG_KEY + teacherKey;
    }

    /**
     * 查询老师
     * 
     * @param id 老师主键
     * @return 老师
     */
    @Override
    public Teacher selectTeacherById(Long id)
    {
        return teacherMapper.selectTeacherById(id);
    }

    /**
     * 查询老师列表
     * 
     * @param teacher 老师
     * @return 老师
     */
    @Override
    public List<Teacher> selectTeacherList(Teacher teacher)
    {
        return teacherMapper.selectTeacherList(teacher);
    }

    /**
     * 新增老师
     * 
     * @param teacher 老师
     * @return 结果
     */
    @Override
    public int insertTeacher(Teacher teacher)
    {
        return teacherMapper.insertTeacher(teacher);
    }

    /**
     * 修改老师
     * 
     * @param teacher 老师
     * @return 结果
     */
    @Override
    public int updateTeacher(Teacher teacher)
    {
        return teacherMapper.updateTeacher(teacher);
    }

    /**
     * 批量删除老师
     * 
     * @param ids 需要删除的老师主键
     * @return 结果
     */
    @Override
    public int deleteTeacherByIds(String ids)
    {
        return teacherMapper.deleteTeacherByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除老师信息
     * 
     * @param id 老师主键
     * @return 结果
     */
    @Override
    public int deleteTeacherById(Long id)
    {
        return teacherMapper.deleteTeacherById(id);
    }

    @Override
    public String importTeacher(List<Teacher> teacherList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(teacherList) || teacherList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Teacher teacher : teacherList)
        {
            try
            {
                // 验证是否存在这个用户
                Teacher u = teacherMapper.selectTeacherByName(teacher.getTeacherName());
                if (StringUtils.isNull(u))
                {
                    teacherMapper.insertTeacher(teacher);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、姓名 " + teacher.getTeacherName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    teacherMapper.updateTeacher(teacher);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、姓名 " + teacher.getTeacherName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、姓名 " + teacher.getTeacherName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、姓名 " + teacher.getTeacherName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
    
    @Override
    public void cleanTeacher()
    {
        teacherMapper.cleanTeacher();
    }

    @Override
    public void resetTeacherCache()
    {
        clearTeacherCache();
        loadingTeacherCache();
    }

    @Override
    public void clearTeacherCache()
    {
        CacheUtils.removeAll(getCacheName());
    }

    @Override
    public void loadingTeacherCache()
    {
        List<Teacher> teachersList = teacherMapper.selectTeacherList(new Teacher());
        for (Teacher teacher : teachersList)
        {
            CacheUtils.put(getCacheName(), getCacheKey(teacher.getTeacherKey()), teacher.getTeacherValue());
        }
    }
}
